%%%-------------------------------------------------------------------
%%% File    : rdf_vect_factory.erl
%%% Author  :  <vjache>
%%% Description : 
%%%
%%% Created : 29 Apr 2009 by  <vjache>
%%%-------------------------------------------------------------------
-module(rdf_vect_factory).

-include("../include/vms.hrl").
-include("../include/vect.hrl").
-include("../include/rdf_record.hrl").
-include("../include/vect_ht.hrl").

%% API
-export([
		 create/4,
		 create/2,
		 vect_root_dir/0,
		 gspo2spog/1,
		 ospg2spog/1,
		 psog2spog/1,
		 spog2gspo/1,
		 spog2ospg/1,
		 spog2psog/1
		 ]).
%% -export([
%% 		 test/0,
%% 		 test/1
%% 		 ]).

%%====================================================================
%% API
%%====================================================================

%%--------------------------------------------------------------------
%% Function: create(Name,RdfVectType,Persistence,Locality,FragProps)
%% Description:
%%--------------------------------------------------------------------
create(Name,RdfVectType) when is_atom(Name) and
							  (RdfVectType==quad_spog orelse
							   RdfVectType==quad_psog orelse
							   RdfVectType==quad_ospg orelse
							   RdfVectType==quad_gspo) ->
	RootDir={?MODULE,vect_root_dir,[]},
	DiskLogPoolName=disk_log_pool,
	QueueName=h3,
	FileName="quadA4L3.log",
	H3Conf=htree:config_std(
			 RootDir, FileName, QueueName, DiskLogPoolName),
	Arity=4,
	#?MANAGED_VECTOR_TAG{
		 name=Name,
		 type=RdfVectType,
		 vect_module=vect_h3,
		 new_arg_list=[H3Conf,Arity,Name]
	}.

create(Name,quad_spog=RdfVectType,Buckets,HtName) ->
	View=#view{vect_name=Name},
	#?MANAGED_VECTOR_TAG{
		 name=Name,
		 type=RdfVectType,
		 vect_module=vect_ht,
		 new_arg_list=[HtName,{?MODULE,vect_root_dir,[]},_Arity=4,Buckets,View]
	};
create(Name,quad_psog=RdfVectType,Buckets,HtName) ->
	View=#view{vect_name=Name,key_pos=2,ext_fun=fun spog2psog/1,int_fun=fun psog2spog/1},
	#?MANAGED_VECTOR_TAG{
		 name=Name,
		 type=RdfVectType,
		 vect_module=vect_ht,
		 new_arg_list=[HtName,{?MODULE,vect_root_dir,[]},_Arity=4,Buckets,View]
	};
create(Name,quad_ospg=RdfVectType,Buckets,HtName) ->
	View=#view{vect_name=Name,key_pos=3,ext_fun=fun spog2ospg/1,int_fun=fun ospg2spog/1},
	#?MANAGED_VECTOR_TAG{
		 name=Name,
		 type=RdfVectType,
		 vect_module=vect_ht,
		 new_arg_list=[HtName,{?MODULE,vect_root_dir,[]},_Arity=4,Buckets,View]
	};
create(Name,quad_gspo=RdfVectType,Buckets,HtName) ->
	View=#view{vect_name=Name,key_pos=4,ext_fun=fun spog2gspo/1,int_fun=fun gspo2spog/1},
	#?MANAGED_VECTOR_TAG{
		 name=Name,
		 type=RdfVectType,
		 vect_module=vect_ht,
		 new_arg_list=[HtName,{?MODULE,vect_root_dir,[]},_Arity=4,Buckets,View]
	}.

psog2spog(?TPSOG) ->
	?TSPOG.
ospg2spog(?TOSPG) ->
	?TSPOG.
gspo2spog(?TGSPO) ->
	?TSPOG.

spog2psog(?TSPOG) ->
	?TPSOG.
spog2ospg(?TSPOG) ->
	?TOSPG.
spog2gspo(?TSPOG) ->
	?TGSPO.

vect_root_dir() ->
	{ok,DeepList}=init:get_argument(dfs),
	OutDir=lists:flatten(DeepList),
	filename:join([OutDir,atom_to_list(node()),"vect/"]).

%%====================================================================
%% Internal functions
%%====================================================================

%% test() ->
%% 	MVect=create(psog,quad_psog,2),
%% 	Vect=apply(MVect#?MANAGED_VECTOR_TAG.vect_module, 
%% 		  new, 
%% 		  MVect#?MANAGED_VECTOR_TAG.new_arg_list),
%% 	Sess=vect:open(Vect),
%% 	ok=vect:insert(Sess,[
%% 						 {psog,{"1",Suf},{"2",Suf},{"3",Suf},{"4",now()}}
%% 						||Suf<-lists:seq(1, 100)
%% 						]),
%% %	iter:print(vect:all(Sess)),
%% 	iter:print(vect:lookup(Sess,{"1",77})),
%% 	ok%=vect:close(Sess)
%% .
%% test(bulk) ->
%% 	MVect=create(psog,quad_psog,2),
%% 	Vect=apply(MVect#?MANAGED_VECTOR_TAG.vect_module, 
%% 		  new, 
%% 		  MVect#?MANAGED_VECTOR_TAG.new_arg_list),
%% 	Iter=iter_mapper:new(iter_term_reader:new_from_file("D:/_Development/Erlang/wsp/demetra/test/valg_bulk_SUITE_data/triple.txt"),
%% 						 fun({S,P,O})->
%% 								 {psog,P,S,O,graph}
%% 						 end),
%% 	io:format("Is vect ~p ~p~n",[Vect,is_record(Vect,vect)]),
%% 	Pipe=pipe_vect:new(Vect),
%% 	Pump=pump:new(Iter, Pipe),
%% 	ok=pump:init(Pump);
%% test(read_all) ->
%% 	MVect=create(psog,quad_psog,2),
%% 	Vect=apply(MVect#?MANAGED_VECTOR_TAG.vect_module, 
%% 		  new, 
%% 		  MVect#?MANAGED_VECTOR_TAG.new_arg_list),
%% 	Sess=vect:open(Vect),
%% 	iter:print(vect:all(Sess)).

